package com.heima.article.controller.v1;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.apis.article.AuthorControllerApi;
import com.heima.article.service.AuthorService;
import com.heima.model.article.pojos.ApArticleContent;
import com.heima.model.article.pojos.ApAuthor;
import com.heima.model.common.dtos.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/v1/author")
public class AuthorController implements AuthorControllerApi {
    /**
     * 根据用户id查询作者信息
     *
     * @param id
     * @return
     */
    @Autowired
    private AuthorService authorService;

    @GetMapping("/findByUserId/{id}")
    @Override
    public ApAuthor findByUserId(@PathVariable("id") Integer id) {

        return authorService.findByUserId(id);
    }

    /**
     * 保存作者
     *
     * @param apAuthor
     * @return
     */
    @PostMapping("save")
    @Override
    public ResponseResult save(@RequestBody ApAuthor apAuthor) {
        ResponseResult responseResult = authorService.insert(apAuthor);
        return responseResult;
    }

    /**
     * 根据名称查询作者
     *
     * @param name
     * @return
     */
    @GetMapping("/findByName/{name}")
    @Override
    public ApAuthor selectAuthorByName(@PathVariable("name") String name) {
        LambdaQueryWrapper<ApAuthor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ApAuthor::getName, name);
        return authorService.getOne(wrapper);
    }

    /**
     * 根据Id查询作者
     *
     * @param id
     * @return
     */
    @GetMapping("/one/{id}")
    @Override
    public ApAuthor findById(@PathVariable("id") Integer id) {
        return authorService.getById(id);
    }
}
